<div class="row-content am-cf" ng-controller="adsControl">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-body">
                	<!--网格行限制宽度-->
					<div class="am-g">
						<div class="am-u-sm-12">
							<div class="widget-head am-cf">
		                        <div class="widget-title am-fl">广告管理</div>
		                        <div class="am-btn-group am-btn-group-xs am-fr">
	                                <!--<a class="am-btn am-btn-default am-btn-success upload-file" href="javascript:void(0)">-->
	                                <a class="am-btn am-btn-default am-btn-success" href="javascript:void(0)" ng-click="form()">
	                                    <span class="am-icon-plus"></span> 新增
	                                </a>
	                                <div class="uploader-list am-cf"></div>
	                            </div>
		                    </div>
						</div>
					</div>
                    <div class="am-g">
                    	<div class="am-u-sm-12">
							<table class="am-table am-table-hover am-table-centered">
							<!--<table class="am-table am-table-hover">-->
								<thead>
									<tr>
										<th>图片</th>
										<th>链接</th>
										<th>广告类型</th>
										<th>广告位置</th>
										<th>广告名称</th>
										<th>广告APPID</th>
										<th>创建时间</th>
										<th>操作</th>
									</tr>
								</thead>
								<tbody>
									<?php if (!$list->isEmpty()): foreach ($list as $item): ?>
									<tr>
										<td class="am-text-middle">
											<a href="##" class="am-list-item-hd ">
												<img src="<?=$item['img']?>" height="50"/>
											</a>
										</td>
										<td class="am-text-middle"><?=(isset($item['href'])) ? $item['href'] : ''?></td>
										
										<!--<td class="am-text-middle"><?=($item['is_open_jump_ads'] == 1) ? '是' : '否'?></td>-->
										<td class="am-text-middle">
											<?php if($item['type1'] == 0) echo '站内';?>
											<?php if($item['type1'] == 1) echo '站外';?>
											<?php if($item['type1'] == 2) echo 'banner广告';?>
											<?php if($item['type1'] == 3) echo '格子广告';?>
										</td>
										<td class="am-text-middle">
											<?php if($item['type'] == 0) echo '砍价首页广告'; ?>
											<?php if($item['type'] == 1) echo '砍价祥情页广告'; ?>
										</td>
										<td class="am-text-middle"><?=$item['jump_ads_name']?></td>
										<td class="am-text-middle"><?=$item['jump_ads_appid']?></td>
										
										<td class="am-text-middle"><?=date('Y-m-d',$item['date'])?></td>
										<td class="am-text-middle">
											<div class="tpl-table-black-operation">
												<a href="javascript:void(0)" ng-click="form('<?=base64_encode(json_encode($item))?>')">
													<i class="am-icon-pencil"></i> 编辑
												</a>
												<a href="javascript:void(0)" class="item-delete tpl-table-black-operation-del" ng-click="del('<?=$item['id']?>')">
													<i class="am-icon-trash"></i> 删除
												</a>
											</div>
										</td>
									</tr>
									<?php endforeach; else: ?>
									<tr>
	                                    <td colspan="9" class="am-text-center">暂无记录</td>
	                                </tr>
                            		<?php endif; ?>
								</tbody>
							</table>
                    	</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--表单-->
	<div class="ads-form" ng-show="isOpenForm" style="margin-top:35px;" ng-cloak>
    	<div class="am-u-sm-10 am-u-sm-centered">
    		<div class="am-panel am-panel-default">
				<!--<div class="am-panel-hd">面板标题</div>-->
				<div class="am-panel-bd">
				    <form class="am-form am-form-horizontal">
						<fieldset>
							<!--<legend>表单标题</legend>-->
							<div class="am-form-group am-form-file">
								<label for="doc-ipt-3" class="am-u-sm-2 am-form-label">图片</label>
								<div class="am-u-sm-10">
									<button type="button" class="am-btn am-btn-default am-btn-sm">
									<i class="am-icon-cloud-upload"></i> 选择要上传的文件</button>
									<input type="file" multiple accept="image/*" name="iFile" ng-model="save.file" onchange="angular.element(this).scope().upload(this.files)">
								</div>
								<!--图片预览-->
								<div class="img" ng-show="save.img != ''">
									<img src="{{save.img}}" height="40" style="margin:8px;">
								</div>
							</div>
							<!--广告类型-->
							 <div class="am-form-group" ng-init="save.type1=0">
							 	<label for="doc-ipt-3" class="am-u-sm-2 am-form-label">广告类型</label>
                                <div class="am-u-sm-10">
									<div class="am-form-group">
										<label class="am-radio-inline">
											<input type="radio" name="type1" value="0" ng-model="save.type1" ng-checked="save.type1 == 0">站内
										</label>
										<label class="am-radio-inline">
											<input type="radio" name="type1" value="1" ng-model="save.type1" ng-checked="save.type1 == 1"> 站外
										</label>
										<label class="am-radio-inline">
											<input type="radio" name="type1" value="2" ng-model="save.type1" ng-checked="save.type1 == 2"> banner广告
										</label>
										<label class="am-radio-inline">
											<input type="radio" name="type1" value="3" ng-model="save.type1" ng-checked="save.type1 == 3"> 格子广告
										</label>
									</div>
                                    <div class="help-block">
                                        <small>选择开启后请填写APPID以及路径，点击砍价按钮后会跳转到其它小程序。</small>
                                    </div>
                                </div>
                            </div>
                            <!--广告位置-->
							 <div class="am-form-group" ng-init="save.type=0">
							 	<label for="doc-ipt-3" class="am-u-sm-2 am-form-label">广告位置</label>
                                <div class="am-u-sm-10">
									<div class="am-form-group">
										<label class="am-radio-inline">
											<input type="radio" name="type" value="0" ng-model="save.type" ng-checked="save.type == 0"> 砍价首页广告
										</label>
										<label class="am-radio-inline">
											<input type="radio" name="type" value="1" ng-model="save.type" ng-checked="save.type == 1"> 砍价祥情页广告
										</label>
									</div>
                                    <div class="help-block">
                                        <small>选择开启后请填写APPID以及路径，点击砍价按钮后会跳转到其它小程序。</small>
                                    </div>
                                </div>
                            </div>
                            <!--跳转广告名-->
							<div class="am-form-group" ng-show="save.type1 == 1">
								<label for="doc-ipt-3" class="am-u-sm-2 am-form-label">广告名称</label>
								<div class="am-u-sm-10">
									<input type="text" id="doc-ipt-3" placeholder="输入跳转广告名称" name="jump_ads_name" ng-model="save.jump_ads_name">
								</div>
							</div>
							
							<!--跳转的小程序appid-->
							<div class="am-form-group" ng-show="save.type1 == 1">
								<label for="doc-ipt-3" class="am-u-sm-2 am-form-label">APPID</label>
								<div class="am-u-sm-10">
									<input type="text" id="doc-ipt-3" placeholder="输入跳转的小程序APPID" name="jump_ads_appid" ng-model="save.jump_ads_appid">
								</div>
							</div>
							
							<!--跳转地址-->
							<div class="am-form-group" ng-show="save.type1 == 0 || save.type1 == 1">
								<label for="doc-ipt-3" class="am-u-sm-2 am-form-label">跳转地址</label>
								<div class="am-u-sm-10">
									<input type="text" id="doc-ipt-3" placeholder="输入跳转地址" class="href" name="href" ng-model="save.href">
								</div>
							</div>
							<div class="am-form-group">
								<div class="am-u-sm-10 am-u-sm-offset-2">
									<button type="submit" class="am-btn am-btn-default" ng-click="submit()">保存</button>
								</div>
							</div>
						</fieldset>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 图片文件列表模板 -->
{{include file="layouts/_template/tpl_file_item" /}}
<!-- 文件库弹窗 -->
{{include file="layouts/_template/file_library" /}}

<script src="https://cdn.bootcss.com/angular.js/1.7.9/angular.min.js"></script>
<script src="assets/common/plugins/layer/layer.js"></script>
<script src="assets/common/js/ddsort.js"></script>
<!--[if IE]>
<script src="https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-btoa-encode-decode/base64-polyfill.js"></script>
<![endif]-->

<script>
	var app = angular.module('app', []);
	app.controller('adsControl', ['$scope','$http',function($scope,$http) {
		$scope.isOpenForm = false;
		$scope.isUpdate = false;
		$scope.save = {}; // 保存的数据
		// $scope.file_id = 0; // 文件id
		// 显示表单
		$scope.form = function(data = null){
			$scope.isUpdate = false;
			$scope.isOpenForm = true;
			if(data != null){
				$scope.isUpdate = true;
				$scope.save = JSON.parse(window.atob(data));
				$scope.save.href = $scope.save.href.replace(/amp;/g, '');
			}else{
				$scope.save = {type: 0,type1: 0,img:''};
			}
			
			layer.open({
				type: 1,
				skin: 'layui-layer-rim', //加上边框
				area: ['800px', '600px'], //宽高
				content: $('.ads-form')
			});
			console.log($scope.save);
		}
		// 文件上传
		$scope.upload = function(files){
			// $scope.file_path = '';
			let fd = new FormData();
			Object.values(files).forEach((v,k)=>{
				// console.log(v);
				fd.append('id','WU_FILE_'+k);
				fd.append('name',v['name']);
				fd.append('type',v['type']);
				fd.append('lastModifiedDate',v['lastModifiedDate']);
				fd.append('size',v['size']);
				fd.append('group_id','10006');
				fd.append('iFile',v);
				
				$http.post(window.location.origin+'/index.php?s=/store/upload/image',fd,{headers:{'Content-Type':undefined }}).then(res=>{
					// console.log(res);
					$scope.save.img = res['data']['data']['file_path'];
					$scope.save.file_id = res['data']['data']['file_id'];
					// $('.img').html('').append('<img src="'+$scope.item.img+'" height="40" style="margin:8px;">');
				});
			})
			
		}
		// 删除
		$scope.del = function(id){
			if(confirm('确定要删除吗？')){
				$http.post(window.location.origin+'/index.php?s=/store/wxapp.page/ads_del',{id:id}).then(res=>{
					// console.log(res);
					if(res.data == 1){
						alert('删除成功');
						window.location.reload();
						return false;
					}
					if(res.data == 0){
						alert('当前数据已删除');
						window.location.reload();
						return false;
					}
					alert('删除失败');
				});
			}
		}
		// 保存
		$scope.submit = function(){
			// let href = $('.href').val();
			console.log($scope.save);
			
			if($scope.isUpdate == false && !$scope.save.img){
				alert('请先上传图片');
				return false;
			}
			if($scope.save.is_open_jump_ads == 1){
				if(!$scope.save.jump_ads_name){
					alert('请填写广告名称');
					return false;
				}
				if(!$scope.save.jump_ads_appid){
					alert('请填写跳转的小程序APPID');
					return false;
				}
			}
			if(!$scope.save.href){
				alert('请填写跳转路径');
				return false;
			}
			if($scope.isUpdate == true && !$scope.save.id){
				alert('缺少id');
				return false;
			}
			
			$http.post(window.location.origin+'/index.php?s=/store/wxapp.page/ads_edit',$scope.save,{}).then(res=>{
				console.log(res);
				window.location.reload();
				layer.closeAll();
			});
		}
			
	}]);
	
	$(function(){
		// 选择图片
	    $('.upload-file').selectImages({
	        name: 'goods[images][]'
	        , multiple: true
	    });
	
	    // 图片列表拖动
	    $('.uploader-list').DDSort({
	        target: '.file-item',
	        delay: 100, // 延时处理，默认为 50 ms，防止手抖点击 A 链接无效
	        floatStyle: {
	            'border': '1px solid #ccc',
	            'background-color': '#fff'
	        }
	    });
	})
</script>

